package bootstrap

import (
	"github.com/gin-gonic/gin"
	"learn/routes"
	"learn/routes/middlewares"
	"net/http"
	"strings"
)

func SetupRoute(route *gin.Engine) {

	// 注册中间件
	reginMiddleware(route)

	// 注册路由
	routes.ReginRoute(route)
	routes.RegisterAPIRoutes(route)

	// 404 页面
	noFond404(route)

}

func reginMiddleware(route *gin.Engine) {

	route.Use(
		middlewares.Logger(),
		middlewares.Recovery(),
	)
}

func noFond404(route *gin.Engine) {

	route.NoRoute(func(c *gin.Context) {
		accept := c.Request.Header.Get("Accept")

		if strings.Contains(accept, "text/html") {
			c.String(http.StatusNotFound, "没找到页面!")
		} else {
			c.JSON(http.StatusNotFound, gin.H{
				"code": http.StatusNotFound,
				"msg":  "没找到页面!",
			})
		}
	})
}
